package com.wdb.user.controller;

import com.google.common.collect.Maps;
import com.wdb.common.configurer.authority.AuthorityExecutor;
import com.wdb.common.configurer.authority.AuthorityProperties;
import com.wdb.common.configurer.authority.AuthorityResponse;
import com.wdb.common.configurer.subject.SubjectTarget;
import com.wdb.common.constant.Constant;
import com.wdb.common.ex.BusinessException;
import com.wdb.common.sequence.IdWorker;
import com.wdb.domain.user.entity.UserInfo;
import com.wdb.domain.user.entity.UserInfoPin;
import com.wdb.domain.user.request.UserPinLoginRequest;
import com.wdb.domain.user.response.SubjectAttrResponse;
import com.wdb.user.repository.UserInfoPinRepository;
import com.wdb.user.repository.UserInfoRepository;
import com.wdb.user.service.UserInfoService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
public class UserInfoController implements UserInfoService {

    @Resource
    private AuthorityProperties properties;
    @Resource
    private AuthorityExecutor executor;


    @Resource
    private UserInfoRepository userInfoRepository;

    @Resource
    private UserInfoPinRepository userInfoPinRepository;

    @Override
    public Map<String,Object> findUserInfo(@RequestParam Long id) {

        try {

            for (int a = 1 ; a <= 10 ; a ++){
                Thread.sleep(1000);
                System.out.println(a);
            }

            UserInfo userInfo = userInfoRepository.find(id);

            if (userInfo == null){
                throw new BusinessException("没有查询到数据");
            }

            Map<String,Object> map = new HashMap<>();

            map.put("name",userInfo.getNickname());
            map.put("mobile",userInfo.getMobile());

            return map;

        }catch (Exception e){
            e.printStackTrace();
        }

        UserInfo userInfo = userInfoRepository.find(id);

        if (userInfo == null){
            throw new BusinessException("没有查询到数据");
        }

        Map<String,Object> map = new HashMap<>();

        map.put("name",userInfo.getNickname());
        map.put("mobile",userInfo.getMobile());

        return map;
    }

    @Override
    public Map<String, Object> saveUserInfo(@RequestBody UserInfo userInfo) {


        UserInfo add = userInfoRepository.add(userInfo);

        Map<String,Object> map = new HashMap<>();

        map.put("name",add.getNickname());
        map.put("mobile",add.getMobile());

        return map;
    }

    @Override
    public AuthorityResponse login(@RequestBody UserPinLoginRequest request) {

        UserInfoPin user = userInfoPinRepository.findByMobile(request.getMobile());

        if (Objects.isNull(user)){
            throw new BusinessException("这个瓜皮不存在");
        }

        Long id = user.getId();

        // 封装返回数据
        SubjectAttrResponse attr = SubjectAttrResponse.builder()
                .uid(id)
                .bound(Constant.Y).build();

        return generate(id, attr);
    }


    private AuthorityResponse generate(Long id, SubjectAttrResponse attr) {
        if (Objects.isNull(id)) {
            return new AuthorityResponse(null, null, attr);
        }

        HashMap<String, Object> claims = Maps.newHashMap();

        claims.put(Constant.Context.SUBJECT, id);

        claims.put(Constant.Context.TARGET, SubjectTarget.USER.name());

        LocalDateTime now = LocalDateTime.now();

        Date iat = Date.from(now.atZone(ZoneId.systemDefault()).toInstant());

        Date exp = Date.from(now.plusSeconds(properties.getActive()).atZone(ZoneId.systemDefault()).toInstant());

        String access = executor.compact(claims, iat, exp);

        exp = Date.from(now.plusSeconds(properties.getRefresh()).atZone(ZoneId.systemDefault()).toInstant());

        String refresh = executor.compact(claims, iat, exp);

        return new AuthorityResponse(access, refresh, attr);
    }

    @Override
    public UserInfoPin register(@RequestBody UserPinLoginRequest request) {

        UserInfoPin add = userInfoPinRepository.add(UserInfoPin.build().mobile(request.getMobile()).password("123456"));

        return add;
    }
}
